package org.neuedu.his.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.neuedu.his.entity.ConstantCategory;
import org.neuedu.his.response.ResponseEntity;
import org.neuedu.his.service.IConstantCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 常数类别 常数类别 前端控制器
 * get  del  edit  list  save
 * </p>
 *
 * @author CDHong
 * @since 2020-03-30
 */
@Api(tags = "常数类别模块")
@RestController
@RequestMapping("/constant-category")
public class ConstantCategoryController {

    @Autowired
    private IConstantCategoryService categoryService;


    /**
     * private RedisTemplate<String,String> stringRedisTemplate;
     * private RedisTemplate<Object,Object> objectRedisTemplate;
     * @apiNote : springBoot 提供整合 redis 的工具类，用于存取操作
     */
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RedisTemplate<Object, Object> objectRedisTemplate;

    @GetMapping("/set")
    public ResponseEntity set(){
        String token = UUID.randomUUID().toString();
        //在 Redis 中存储一个 token 值, 失效时间 5秒
        redisTemplate.opsForValue().set("token",token,5, TimeUnit.SECONDS);
        List<ConstantCategory> list = categoryService.list();
        objectRedisTemplate.opsForValue().set("constant-category",list,5, TimeUnit.SECONDS);
        return ResponseEntity.ok("存储token成功~");
    }

    @GetMapping("/get")
    public ResponseEntity get(){
        String token = redisTemplate.opsForValue().get("token");
        List<ConstantCategory> list = (List<ConstantCategory>) objectRedisTemplate.opsForValue().get("constant-category");
        System.out.println(list);
        return ResponseEntity.ok(token);
    }


    @ApiOperation("根据常数类别编码查询所有类别")
    @GetMapping("/{code}")
    public ResponseEntity getCategoryByConstantCode(@PathVariable String code){
        List<ConstantCategory> list = categoryService.lambdaQuery().eq(ConstantCategory::getConstantCode, code).list();
        return ResponseEntity.data(list);
    }

    @ApiOperation("查询所有的常数类别")
    @GetMapping
    public ResponseEntity listConstantCategory(){
        List<ConstantCategory> list = categoryService.list();
        return ResponseEntity.data(list);
    }

    @ApiOperation("保存常数类别")
    @PostMapping
    public ResponseEntity saveConstantCategory(@RequestBody ConstantCategory constantCategory){
        boolean flg = categoryService.save(constantCategory);
        if(flg){
            return ResponseEntity.ok("添加成功~");
        }
        return ResponseEntity.error("添加失败~");
    }

    @ApiOperation("根据主键ID编辑常数类别")
    @PutMapping
    public ResponseEntity editById(ConstantCategory constantCategory){
        boolean flg = categoryService.updateById(constantCategory);
        if(flg){
            return ResponseEntity.ok("修改成功~");
        }
        return ResponseEntity.error("修改失败~");
    }

    @ApiOperation("根据主键ID删除常数类别")
    @ApiImplicitParam(name = "id",value = "常数类别主键ID")
    @DeleteMapping("/{id}")
    public ResponseEntity delById(@PathVariable Integer id){
        boolean flg = categoryService.removeById(id);
        if(flg){
            return ResponseEntity.ok("删除成功~");
        }
        return ResponseEntity.error("删除失败~");
    }


}
